/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dns_option_napi.h"
#include "imageknife_handle_scope.h"
#include "napi_parser.h"
namespace ImageKnifePro {

DnsOptionNapi::DnsOptionNapi(napi_env env, napi_value dnsNapi)
{
    napi_value staticDnsRuleNapi = NapiParser::ParseProperty(env, dnsNapi, "staticDnsRule");
    if (staticDnsRuleNapi == nullptr) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "DnsOptionRcpNapi ParseProperty staticDnsRule failed");
        return;
    }

    uint32_t length = 0;
    napi_status status = napi_get_array_length(env, staticDnsRuleNapi, &length);
    if (status != napi_ok) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "DnsOptionRcpNapi napi_get_array_length failed");
        return;
    }

    ImageKnifeHandleScope scope(env);
    for (int i = 0; i < length; i++) {
        napi_value element;
        napi_get_element(env, staticDnsRuleNapi, i, &element);
        StaticDnsRuleItem item;

        napi_value value = NapiParser::ParseProperty(env, element, "host");
        if (value == nullptr) {
            IMAGE_KNIFE_LOG(LOG_ERROR, "DnsOptionRcpNapi Parse property host failed at index:%{public}d", i);
            continue;
        }
        item.host = NapiParser::ParseString(env, value);

        value = NapiParser::ParseProperty(env, element, "port");
        if (value == nullptr) {
            IMAGE_KNIFE_LOG(LOG_DEBUG, "DnsOptionRcpNapi Parse property port failed at index:%{public}d", i);
            continue;
        }
        item.port = NapiParser::ParseInt(env, value);

        value = NapiParser::ParseProperty(env, element, "ipAddresses");
        if (value == nullptr) {
            IMAGE_KNIFE_LOG(LOG_DEBUG, "DnsOptionRcpNapi Parse property ipAddresses failed at index:%{public}d", i);
            continue;
        }

        ParseIpAddresses(env, value, item.ipAddresses);
        if (!item.ipAddresses.empty()) {
            staticDnsRule_.emplace_back(std::move(item));
        }
    }
    CovertRcpStaticDnsRule();
}

void DnsOptionNapi::ParseIpAddresses(napi_env env, napi_value jsArr, std::vector<std::string> &ipAddresses)
{
    uint32_t length = 0;
    napi_status status = napi_get_array_length(env, jsArr, &length);
    if (status != napi_ok) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "DnsOptionRcpNapi IpAddresses napi_get_array_length failed");
        return;
    }

    for (int i = 0; i < length; i++) {
        napi_value element;
        napi_get_element(env, jsArr, i, &element);
        ipAddresses.emplace_back(NapiParser::ParseString(env, element));
    }
}

}